package com.whitedust.top90;

import com.whitedust.structure.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author A
 * @since 2025/11/16 21:04
 */

public class Top84 {
    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        List<Integer> path = new ArrayList<>();
        List<List<Integer>> res = new ArrayList<>();
        if(root==null)return res;
        path.add(root.val);
        pathSum1(root,targetSum-root.val,path,res);
        return res;
    }

    private void pathSum1(TreeNode root, int targetSum, List<Integer> path, List<List<Integer>> res) {
        if (root.left == null && root.right == null && targetSum == 0) {
            res.add(new ArrayList<>(path));
            return;
        } else if (root.left == null && root.right == null) {
            return;
        }
        if (root.left != null) {
            path.add(root.left.val);
            pathSum1(root.left, targetSum - root.left.val, path, res);
            path.remove(path.size() - 1);
        }
        if (root.right != null) {
            path.add(root.right.val);
            pathSum1(root.right, targetSum - root.right.val, path, res);
            path.remove(path.size() - 1);
        }
    }
}
